package com.group.activity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.activity.dao.BargainListDao;
import com.group.activity.entity.ShopCommodityEntity;
import com.group.activity.vo.BargainGoodsVo;
import com.group.activity.vo.BargainQueryVo;
import com.group.common.utils.R;
import com.group.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.common.utils.PageUtils;
import com.group.common.utils.Query;

import com.group.activity.dao.BargainGoodsDao;
import com.group.activity.entity.BargainGoodsEntity;
import com.group.activity.service.BargainGoodsService;


@Service("bargainGoodsService")
public class BargainGoodsServiceImpl extends ServiceImpl<BargainGoodsDao, BargainGoodsEntity> implements BargainGoodsService {
    @Autowired
    private BargainGoodsDao bargainGoodsDao;
    @Autowired
    private BargainListDao bargainListDao;
    @Autowired
    private RedisTemplate redisTemplate;
    //校验注入
    @Autowired
    private StringUtils stringUtils;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BargainGoodsEntity> page = this.page(
                new Query<BargainGoodsEntity>().getPage(params),
                new QueryWrapper<BargainGoodsEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageInfo<BargainGoodsVo> bargainGoodsList(BargainQueryVo bargainQueryVo) {
        PageHelper.startPage(bargainQueryVo.getPage(),bargainQueryVo.getSize());
        List<BargainGoodsVo> bargainGoodsVos=bargainGoodsDao.bargainGoodsList(bargainQueryVo);

        return new PageInfo<>(bargainGoodsVos);
    }

    /*
    * 添加砍价商品
    * */
    @Override
    public R addBargainItem(BargainGoodsEntity bGoodsEntity) throws ParseException {
        //校验值不能为空
        if (stringUtils.isEmpty(bGoodsEntity.getBargainGoodsName())){
            return R.error("请录入砍价活动名称");
        }

        if (stringUtils.isEmpty(bGoodsEntity.getBargainGoodsBrief())){
            return R.error("请录入砍价活动简介");
        }

        if (stringUtils.isVerification(bGoodsEntity.getBargainGoodsNuminvitation())){
            return R.error("请录入砍价次数");
        }

        if (stringUtils.isVerification(bGoodsEntity.getBargainingNumber())){
            return R.error("请录入砍价人数");
        }

        if (stringUtils.isVerification(bGoodsEntity.getBargainGoodsPurchafreque())){
            return R.error("请录入购买数量限制");
        }

        if (stringUtils.isVerification(bGoodsEntity.getBargainGoodsFreight())){
            return R.error("请录入运费模板");
        }

        if (stringUtils.isVerification(bGoodsEntity.getBargainGoodsState())){
            return R.error("请录入活动状态");
        }

        if (stringUtils.isVerification(bGoodsEntity.getBargainGoodsLimitshu())){
            return R.error("请录入商品限量");
        }

        if (stringUtils.isVerification(bGoodsEntity.getShopId())){
            return R.error("请录入商品id");
        }
        ShopCommodityEntity shopCommodityEntity = bargainListDao.shopList(bGoodsEntity.getShopId());
        if (shopCommodityEntity==null){
            return R.error("当前商品不存在");
        }
        if (stringUtils.isTimeVerification(bGoodsEntity.getBargainGoodsStarttime())){
            return R.error("请录入开始时间");
        }

        if (stringUtils.isTimeVerification(bGoodsEntity.getBargainGoodsEndtime())){
            return R.error("请录入结束时间");
        }
        //获取当前系统时间
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //转换为date类型
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newDate = simpleDateFormat.parse(now);

        //时间比较 开始时间不能大于当前时间
        int i1 = newDate.compareTo(bGoodsEntity.getBargainGoodsStarttime());
        if (i1>0){
            return R.error("开始时间不能小于当前时间");
        }
        //结束时间不能大于开始时间
        int i = bGoodsEntity.getBargainGoodsStarttime().compareTo(bGoodsEntity.getBargainGoodsEndtime());
        if (i>0){
            return R.error("结束时间不能小于开始时间");
        }
        bGoodsEntity.setBargainGoodsLimitedsurplus(bGoodsEntity.getBargainGoodsLimitshu());
        //判断该商品是否存在
        BargainGoodsEntity shop_id = baseMapper.selectOne(new QueryWrapper<BargainGoodsEntity>().eq("shop_id",bGoodsEntity.getShopId()));
        if (shop_id!=null){
            return R.error("选择商品已存在 请重新选择");
        }
        int insert = baseMapper.insert(bGoodsEntity);
        if (insert==1){
            //当前添加商品id为key  商品限量value存redis
            redisTemplate.opsForValue().set(bGoodsEntity.getBargainGoodsId()+"",bGoodsEntity.getBargainGoodsLimitshu());
            return R.ok("砍价商品录入成功");
        }
        return R.error("砍价商品录入失败");
    }

}